package leetcode;

import java.util.HashSet;
import java.util.Set;

/**
 * 题目：无重复字符的最长子串
 * 链接：https://leetcode.cn/problems/longest-substring-without-repeating-characters/description/
 * 思路：滑动窗口
 */
public class LengthOfLongestSubstring {
    public static void main(String[] args) {
        String s = "abcbea";
        int i = lengthOfLongestSubstring(s);
        System.out.println(i);
    }
    public static int lengthOfLongestSubstring(String s) {
        int n= s.length();
        int left=0;
        int maxLen=0;
        Set<Character> set =new HashSet<>();
        for(int right=0;right<n;right++){
            char c=s.charAt(right);
            while(set.contains(c)){
                set.remove(s.charAt(left));
                left++;
            }
            set.add(c);
            maxLen=Math.max(maxLen,right-left+1);
        }
        return maxLen;
    }
}
